package com.android.likeweixin;

import java.util.ArrayList;
import java.util.List;

import org.widget.PraiseTextView;
import org.widget.ReplyTextView;
import org.widget.pulltorefresh.PullToRefreshListView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.likeweixin.model.FriendDynamic;
import com.android.likeweixin.model.FriendDynamic.Comment;
import com.android.likeweixin.model.FriendDynamic.Praise;
import com.android.likeweixin.util.Util;

public class FriendCircleListAdapter extends BaseAdapter {
	private List<FriendDynamic> mDynamics;
	private LayoutInflater inflater;
	private Context mContext;
	/**
	 * 赞点击事件
	 */
	private ZanClickListener zanListener = new ZanClickListener();
	/**
	 * 评论点击事件
	 */
	private CommentClickListener commListener = new CommentClickListener();

	public FriendCircleListAdapter(Context context, List<FriendDynamic> dynamics) {
		mDynamics = new ArrayList<FriendDynamic>();
		inflater = LayoutInflater.from(context);
		mContext = context;
		refresh(dynamics);
	}

	public void refresh(List<FriendDynamic> dynamics) {
		if (dynamics != null) {
			mDynamics.clear();
			mDynamics.addAll(dynamics);
			notifyDataSetChanged();
		}
	}

	/**
	 * 刷新评论
	 */
	public void refreshComment(int postion, Comment ct) {
		FriendDynamic fd = mDynamics.get(postion);
		if (fd.mComments == null) {
			fd.mComments = new ArrayList<Comment>();
		}
		fd.mComments.add(ct);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mDynamics.size();
	}

	@Override
	public Object getItem(int position) {
		return mDynamics.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		FriendDynamic fd = mDynamics.get(position);
		convertView = inflater.inflate(R.layout.friend_circle_item, null);
		// 名字内容区域
		final ImageView header = (ImageView) convertView
				.findViewById(R.id.header);
		TextView name = (TextView) convertView.findViewById(R.id.name);
		TextView content = (TextView) convertView.findViewById(R.id.content);
		name.setText(fd.realName);
		content.setText(fd.newsContent);
		// 时间
		TextView timeBefore = (TextView) convertView
				.findViewById(R.id.timeBefore);
		timeBefore.setText(Util.makeUpdateTime(fd.newsTime));
		// 图片区域
		List<String> pics = fd.pics;
		final ImageView pic = (ImageView) convertView.findViewById(R.id.pic);
		if (pics != null && !pics.isEmpty()) {
			if (pics.size() == 1) {
				pic.setVisibility(ImageView.VISIBLE);

			} else {
				GridView picsGridView = (GridView) convertView
						.findViewById(R.id.picsGridView);
				picsGridView.setVisibility(GridView.VISIBLE);
				picsGridView.setAdapter(new PicsGridView(pics));
			}
		}
		// 评论，赞操作区域
		TextView comment = (TextView) convertView.findViewById(R.id.comment);
		TextView zan = (TextView) convertView.findViewById(R.id.zan);
		zan.setOnClickListener(zanListener);
		zan.setTag(fd);
		comment.setOnClickListener(commListener);
		comment.setTag(fd);
		comment.setTag(R.id.fd_postion, mDynamics.indexOf(fd));
		List<Praise> praises = fd.mPraises;
		int index = zanListener.isPraiseByLogin(praises);
		// 我未赞
		if (index <= -1) {
			zan.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources()
					.getDrawable(R.drawable.zan_no), null, null, null);

		} else {// 我已赞
			zan.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources()
					.getDrawable(R.drawable.zan_yes), null, null, null);
		}
		// 赞区域
		if (praises != null && !praises.isEmpty()) {
			PraiseTextView praise = (PraiseTextView) convertView
					.findViewById(R.id.praiser);
			praise.setVisibility(View.VISIBLE);
			praise.setPraisesText(praises);
		}
		// 回复区域
		List<Comment> comments = fd.mComments;
		if (comments != null && !comments.isEmpty()) {
			convertView.findViewById(R.id.replyContent).setVisibility(
					View.VISIBLE);
			ListView reply = (ListView) convertView
					.findViewById(R.id.replyListView);
			reply.setAdapter(new CommentsAdapter(comments));
		}
		return convertView;
	}

	/**
	 * 多张图片gridview adapter
	 */
	private class PicsGridView extends BaseAdapter {

		private List<String> datas;

		public PicsGridView(List<String> data) {
			this.datas = data;
		}

		public int getCount() {
			return datas.size();
		}

		public Object getItem(int position) {
			return datas.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public int getViewTypeCount() {
			return 1;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = inflater.inflate(R.layout.friend_dynamic_pic_item,
					null);
			final ImageView picImage = (ImageView) convertView
					.findViewById(R.id.picImage);
			return convertView;
		}
	}

	/**
	 * 回复adapter
	 */
	private class CommentsAdapter extends BaseAdapter {

		private List<Comment> datas;

		public CommentsAdapter(List<Comment> data) {
			this.datas = data;
		}

		public int getCount() {
			return datas.size();
		}

		public Object getItem(int position) {
			return datas.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public int getViewTypeCount() {
			return 2;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = inflater.inflate(R.layout.friend_dynamic_reply_item,
					null);
			Comment ct = datas.get(position);
			ReplyTextView tv = (ReplyTextView) convertView
					.findViewById(R.id.replyTextView);
			tv.setCommentText(ct);
			// picImage.setImageBitmap(null);
			return convertView;
		}
	}

	private class ZanClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			TextView tv = (TextView) v;
			FriendDynamic fd = (FriendDynamic) tv.getTag();
			int postion = mDynamics.indexOf(fd);
			if (postion != -1) {
				List<Praise> praises = fd.mPraises;
				int index = isPraiseByLogin(praises);
				ListView lv = getListView(v);
				int firstPosition = lv.getFirstVisiblePosition();
				View item = lv.getChildAt(postion - firstPosition + 1);
				// System.out.println("position:"+postion+"fd:"+fd.newsContent+"firstPosition:"+firstPosition);
				PraiseTextView pv = (PraiseTextView) item
						.findViewById(R.id.praiser);
				// 我已赞
				if (index > -1) {
					tv.setCompoundDrawablesWithIntrinsicBounds(mContext
							.getResources().getDrawable(R.drawable.zan_no),
							null, null, null);
					praises.remove(index);
					fd.mPraises = praises;
				} else {// 我未赞
					if (praises == null) {
						praises = new ArrayList<FriendDynamic.Praise>();
					}
					tv.setCompoundDrawablesWithIntrinsicBounds(mContext
							.getResources().getDrawable(R.drawable.zan_yes),
							null, null, null);
					Praise p = new Praise();
					p.realName = "自己";
					p.userId = FriendDynamic.getLoginUserId();
					praises.add(p);
					fd.mPraises = praises;
				}
				if (pv == null)
					return;
				// 赞区域
				if (fd.mPraises != null && !fd.mPraises.isEmpty()) {
					pv.setVisibility(View.VISIBLE);
					pv.setPraisesText(fd.mPraises);
				} else {
					pv.setVisibility(View.GONE);
				}
			}
		}

		int isPraiseByLogin(List<Praise> praises) {
			int index = -1;
			if (praises == null)
				return index;
			for (Praise p : praises) {
				if (FriendDynamic.getLoginUserId().equals(p.userId)) {
					index = praises.indexOf(p);
					break;
				}
			}
			return index;
		}
	}

	/**
	 * 朋友圈评论
	 */
	private class CommentClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// **************************特效区域*****************************/
			View root = v.getRootView();
			final View commentBottom = root.findViewById(R.id.commentBottom);
			commentBottom.setVisibility(View.VISIBLE);
			// 弹出输入法
			final EditText et = (EditText) root
					.findViewById(R.id.et_sendmessage);
			et.requestFocus();
			Util.showInputMethod((Activity) mContext, et);
			// 实现点击edit其他区域，输入法消息,edit失去焦点
			PullToRefreshListView layout = (PullToRefreshListView) root
					.findViewById(R.id.listview);
			final ListView lv = layout.getRefreshableView();
			lv.setFocusable(true);
			lv.setFocusableInTouchMode(true);
			lv.setOnTouchListener(new OnTouchListener() {
				public boolean onTouch(View v, MotionEvent event) {
					lv.setFocusable(true);
					lv.setFocusableInTouchMode(true);
					lv.requestFocus();
					return false;
				}
			});
			et.setOnFocusChangeListener(new View.OnFocusChangeListener() {

				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if (!hasFocus) {
						// 失去焦点
						et.clearFocus();
						et.setText("");
						Util.hideInputMethod(mContext, et);
						commentBottom.setVisibility(View.GONE);
					}
				}
			});
			// **************************设置评论数据tag*****************************/
			FriendDynamic fd = (FriendDynamic) v.getTag();
			Comment c = new Comment();
			c.replyUserId = fd.useId;
			c.replyRealName = fd.realName;
			c.newsId = fd.newsId;
			et.setTag(R.id.fd_postion, v.getTag(R.id.fd_postion));
			et.setTag(R.id.fd_comment, c);
		}
	}

	private ListView getListView(View v) {
		View root = v.getRootView();
		PullToRefreshListView layout = (PullToRefreshListView) root
				.findViewById(R.id.listview);
		final ListView lv = layout.getRefreshableView();
		return lv;
	}
}
